﻿using System;
using System.Collections.Generic;
using System.Linq;
using Framework.Exceptions.RepositoryExceptions;
using Interfaces.Repositories;
using Interfaces.RepositoryModels.InternalModels.NewsInformationModels;

namespace SQLRepositories.NewsRepositories
{
    public class LinkRepository : EntityBaseRepository<LinkModel>, ILinkRepository
    {
        public LinkRepository(IDBEconomicInformationServiceContext context) 
            : base(context)
        {}

        public void Create(LinkModel item)
        {
            Context.Links.Add(item);
            Context.SaveChanges();
        }

        public LinkModel GetById(Guid id)
        {
            return Context.Links.FirstOrDefault(x => x.Id.Equals(id));
        }

        public void Delete(Guid id)
        {
            var removedItems = Context.Links.FirstOrDefault(x => x.Id.Equals(id));
            if(removedItems == null)
                throw new EntityNotFoundException("Link");
            Context.Links.Remove(removedItems);
            Context.SaveChanges();
        }

        public void Delete(LinkModel item)
        {
            Context.Links.Remove(item);
            Context.SaveChanges();
        }

        public IEnumerable<LinkModel> GetAll()
        {
            return Context.Links;
        }
    }
}
